package com.ruoyi.ma.controller;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.*;
import java.util.concurrent.TimeUnit;

import cn.hutool.core.collection.CollUtil;
import com.ruoyi.common.config.RuoYiConfig;
import com.ruoyi.common.excel.ExcelResult;
import com.ruoyi.ma.domain.vo.MaMaterialManifestImportVo;
import com.ruoyi.ma.listener.MaMaterialfestImportListener;
import com.ruoyi.system.domain.vo.SysUserImportVo;
import com.ruoyi.system.listener.SysUserImportListener;
import liquibase.repackaged.org.apache.commons.text.WordUtils;
import lombok.RequiredArgsConstructor;
import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.dev33.satoken.annotation.SaCheckLogin;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.validate.AddGroup;
import com.ruoyi.common.core.validate.EditGroup;
import com.ruoyi.common.core.validate.QueryGroup;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.ma.domain.vo.MaMaterialManifestVo;
import com.ruoyi.ma.domain.bo.MaMaterialManifestBo;
import com.ruoyi.ma.service.IMaMaterialManifestService;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 材料清单
 *
 * @author llb
 * @date 2024-11-01
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/ma/materialManifest")
public class MaMaterialManifestController extends BaseController {

    private final IMaMaterialManifestService iMaMaterialManifestService;

    /**
     * 查询材料清单列表
     */
    @SaCheckLogin
    @GetMapping("/list")
    public TableDataInfo<MaMaterialManifestVo> list(MaMaterialManifestBo bo, PageQuery pageQuery) {
        return iMaMaterialManifestService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出材料清单列表
     */
    @SaCheckLogin
    @Log(title = "材料清单", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(MaMaterialManifestBo bo, HttpServletResponse response) {
        List<MaMaterialManifestVo> list = iMaMaterialManifestService.queryList(bo);
        ExcelUtil.exportExcel(list, "材料清单", MaMaterialManifestVo.class, response);
    }

    /**
     * 获取材料清单详细信息
     *
     * @param manifestId 主键
     */
    @SaCheckLogin
    @GetMapping("/{manifestId}")
    public R<MaMaterialManifestVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long manifestId) {
        return R.ok(iMaMaterialManifestService.queryById(manifestId));
    }

    /**
     * 新增材料清单
     */
    @SaCheckLogin
    @Log(title = "材料清单", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody MaMaterialManifestBo bo) {
        return toAjax(iMaMaterialManifestService.insertByBo(bo));
    }

    /**
     * 批量新增材料清单
     */
    @SaCheckLogin
    @Log(title = "材料清单", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping("/addList")
    public R<Void> addList(@Validated(AddGroup.class) @RequestBody MaMaterialManifestBo bo) {
        return toAjax(iMaMaterialManifestService.addList(bo));
    }

    /**
     * 修改材料清单
     */
    @SaCheckLogin
    @Log(title = "材料清单", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody MaMaterialManifestBo bo) {
        return toAjax(iMaMaterialManifestService.updateByBo(bo));
    }

    /**
     * 批量修改材料清单
     */
    @SaCheckLogin
    @Log(title = "材料清单", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping("/editList")
    public R<Void> editList(@Validated(EditGroup.class) @RequestBody MaMaterialManifestBo bo) {
        return toAjax(iMaMaterialManifestService.editList(bo));
    }

    /**
     * 删除材料清单
     *
     * @param manifestIds 主键串
     */
    @SaCheckLogin
    @Log(title = "材料清单", businessType = BusinessType.DELETE)
    @DeleteMapping("/{manifestIds}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] manifestIds) {
        return toAjax(iMaMaterialManifestService.deleteWithValidByIds(Arrays.asList(manifestIds), true));
    }

    /**
     * 获取下载模板
     */
    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response) {
        ExcelUtil.exportExcel(new ArrayList<>(), "材料清单", MaMaterialManifestImportVo.class, response);
    }

    // 获取清单列表-不分页
    @SaCheckLogin
    @GetMapping("/allList")
    public R<List<MaMaterialManifestVo>> allList(MaMaterialManifestBo bo) {
        return iMaMaterialManifestService.allList(bo);
    }

    /**
     * 导入数据
     *
     * @param file          导入文件
     * @param gysId         供应商id
     */
    @Log(title = "供应商管理", businessType = BusinessType.IMPORT)
    @SaCheckLogin
    @PostMapping(value = "/importData", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    public R<Void> importData(@RequestPart("file") MultipartFile file, @NotNull(message = "供应商不能为空") Long gysId) throws Exception {
        ExcelResult<MaMaterialManifestVo> data = ExcelUtil.importExcel(file.getInputStream(), MaMaterialManifestVo.class, new MaMaterialfestImportListener(gysId));
        return R.ok(data.getAnalysis());
    }

}
